package com.yangff.listener;

import com.yangff.entity.vo.JwtVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class UserListener {


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "user.token.save.queue"),
                    exchange = @Exchange(name = "fts.direct",type = ExchangeTypes.DIRECT),
                    key = "user.token.save"
            )
    )
    @CachePut(value = "usertoken" , key = "#jwtVo.account")
    public JwtVo saveUserToken(JwtVo jwtVo){
        return jwtVo;
    }


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "user.token.delete.queue"),
                    exchange = @Exchange(name = "fts.direct",type = ExchangeTypes.DIRECT),
                    key = "user.token.delete"
            )
    )
    @CacheEvict(value = "usertoken" , key = "#jwtVo.account")
    public void deleteUserToken(JwtVo jwtVo){
        log.debug("{} 已清理", jwtVo.getAccount());
    }

}
